package com.guit.hrms.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.guit.hrms.mapper.*;
import com.guit.hrms.po.statistics.CountRecruitment;
import com.guit.hrms.po.statistics.StatisticalInterviewPassRate;
import com.guit.hrms.po.utilsPO.PageBean;
import com.guit.hrms.service.StatisticsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class StatisticsServiceImpl implements StatisticsService {

    @Autowired
    private StatisticsMapper statisticsMapper;

    /**
     * 统计招聘计划转化率
     *
     * @param page
     * @param pageSize
     * @param planId
     * @param planType
     * @return
     */
    @Override
    public PageBean countRecruitment(Integer page, Integer pageSize, Integer planId, Integer planType) {

        PageHelper.startPage(page, pageSize);
        List<CountRecruitment> countRecruitmentList = statisticsMapper.countRecruitment(planId, planType);
        if (countRecruitmentList == null) {
            return new PageBean(0L, new ArrayList<>());
        }

        int count = 0;
        for (CountRecruitment countRecruitment : countRecruitmentList) {
            if (countRecruitment.getPlanType() == 1) {
                countRecruitment = statisticsMapper.getRecruitmentToDept(countRecruitment.getPlanId());
                double allConversionRate = countRecruitment.getAllApplicantNumber() == 0 ? 0.0 :
                        (countRecruitment.getAllThroughNumber() * 0.01) / countRecruitment.getAllApplicantNumber();
                DecimalFormat df = new DecimalFormat("0.00%");
                String allConversionRateString = df.format(allConversionRate);
                countRecruitment.setAllConversionRate(allConversionRateString);
                countRecruitmentList.set(count, countRecruitment);
            } else if (countRecruitment.getPlanType() == 2) {
                countRecruitment = statisticsMapper.getRecruitmentToAcademy(countRecruitment.getPlanId());
                double allConversionRate = countRecruitment.getAllApplicantNumber() == 0 ? 0.0 :
                        (countRecruitment.getAllThroughNumber() * 0.01) / countRecruitment.getAllApplicantNumber();
                DecimalFormat df = new DecimalFormat("0.00%");
                String allConversionRateString = df.format(allConversionRate);
                countRecruitment.setAllConversionRate(allConversionRateString);
                countRecruitmentList.set(count, countRecruitment);
            }
            count++;
        }

        Page<CountRecruitment> p = (Page<CountRecruitment>) countRecruitmentList;
        return new PageBean(p.getTotal(), p.getResult());
    }

    /**
     * 统计面试通过率
     *
     * @param page
     * @param pageSize
     * @param planId
     * @param planType
     * @param deptId
     * @param positionNumber
     * @return
     */
    @Override
    public PageBean statisticalInterviewPassRate(Integer page, Integer pageSize, Integer planId, Integer planType, Integer interviewId, String deptId, String positionNumber) {
        PageHelper.startPage(page, pageSize);
        List<StatisticalInterviewPassRate> statisticalInterviewPassRateList = statisticsMapper.statisticalInterviewPassRateToDept(planId, planType, interviewId, deptId, positionNumber);
        if (statisticalInterviewPassRateList == null) {
            return new PageBean(0L, new ArrayList<>());
        }
        int count = 0;
        for (StatisticalInterviewPassRate statisticalInterviewPassRate : statisticalInterviewPassRateList) {
            double throughNumber = statisticalInterviewPassRate.getApplicantNumber() == 0 ? 0.0 :
                    statisticalInterviewPassRate.getThroughNumber() * 0.01 / statisticalInterviewPassRate.getApplicantNumber();
            DecimalFormat df = new DecimalFormat("0.00%");
            String throughNumberString = df.format(throughNumber);
            statisticalInterviewPassRate.setConversionRate(throughNumberString);
            statisticalInterviewPassRateList.set(count, statisticalInterviewPassRate);
            count++;
        }
        Page<StatisticalInterviewPassRate> p = (Page<StatisticalInterviewPassRate>) statisticalInterviewPassRateList;
        return new PageBean(p.getTotal(), p.getResult());
    }
}
